import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';
import {message,Row,Col} from 'antd';
import * as  Apis from '../../../apis/govern';
import { WeaTableNew } from 'comsMobx';
import {WeaDateGroup, WeaBrowser,WeaLocaleProvider} from 'ecCom';
import CommentMain from '../dialog/main';
const getLabel = WeaLocaleProvider.getLabel;
const WeaTable = WeaTableNew.WeaTable;

//领导批示组件
@inject('checkMatterStore','proinfoStore')
@observer
export default class GovernInstruct extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            topHeight: 0,
            tab: [getLabel('18329', '领导批示')],
            comment: [getLabel('675','评论')],
            commentFlag: true
        }
    }

    componentDidMount = () => {
        const { governReportStore, id, type, checkMatterStore } = this.props;
        this.getLeaderCommentType({ id });
        governReportStore.getDataList({ id, type });
        let { pageindex, pagesize } = checkMatterStore;
        checkMatterStore.getCheckCondition({ "id": id, "type": type });
        checkMatterStore.getTaskId(id);
        checkMatterStore.init({ "taskId": id, "pageindex": pageindex, "pagesize": pagesize });
    }


    componentWillReceiveProps(nextProps) {
        //刷新重新渲染 lusx 19.2.28 没必要，和下重复
        // if (nextProps.id != this.props.id) {
        //     this.props.governReportStore.getDataList({ id: nextProps.id, type: nextProps.type });
        // }
        //lusx 详情页面，弹框保存后页面不刷新 19.1.30
        if(nextProps.location.key != this.props.location.key){
            const {governReportStore:{isdoSearch}} = this.props;
            if(isdoSearch === true){  //lusx #554258  概述：E9，督办，详情，领导批示、催办信息等类似tab页，查询条件不生效
              return false;
            }else{
              nextProps.governReportStore.getDataList({ id: nextProps.id, type: nextProps.type });
            }
        }
    }
    componentWillUnmount = () => {
      const {governReportStore:{clearData}} = this.props;
      clearData();
    }

    getLeaderCommentType = (params = {}) => {
        let id = params.id;
        Apis.getLeaderCommentType({id}).then(data => {
          if(parseInt(data.status) == 1){
            this.setState({
                commentFlag: data.flag
            });
          }else{
            message.error(data.error);
          }
        }).catch(error => {
            message.error(error);
        })
    }

    render() {
        const { governReportStore, labelTime, labelPers, checkMatterStore} = this.props;
        const { status, tableStoreMyAttent } = governReportStore;
        const { loading } = checkMatterStore;
        const { userInfo, weiboCKEditorConfig, basicInfo, systemWorkLog, weiboGroup, fanList, attentionList, defaultTemplate, keyWords } = toJS(this.props.checkMatterStore.status),
            { topHeight, tab, comment, commentFlag } = this.state;
        //获取评论与汇报列表数据
        const getWeiboList = () => {
            let weiboList = {};
            let dialogWeiboList = toJS(this.props.checkMatterStore.status.weiboList);
            let modelWeiboList = toJS(this.props.proinfoStore.modelStore.status.weiboList);
            if (modelWeiboList) {
                if (modelWeiboList.total > dialogWeiboList.total) {
                    weiboList = _.cloneDeep(modelWeiboList);
                } else {
                    weiboList = _.cloneDeep(dialogWeiboList);
                }
            } else {
                weiboList = _.cloneDeep(dialogWeiboList);
            }
            return weiboList;
        }
        let weiboList = getWeiboList();
        return (
            <div className='govern-instruct govern-report'>
                <div hidden={!commentFlag}>
                    <div className="g-clearfix">
                        <div className="g-search-condition">
                            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@3ngt1n`} type='flex' gutter={16}>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@r8mgll`}>
                                    <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@fz77bv`} type='flex' gutter={24}>
                                        <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@onlqdw`}>
                                            <div className="search-title">{labelPers}:</div>
                                        </Col>
                                        <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@6gcfdg`}>
                                            <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@e2y8fy`}
                                              type={17}
                                              textDecoration={true}
                                              inputStyle={{width: 200}}
                                              onChange={(ids, names, datas)=> this.browserHandle(ids)}
                                              isSingle={false}
                                              title={getLabel(106, "对象")}
                                            />
                                        </Col>
                                    </Row>
                                </Col>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@g3sguf`}>
                                    <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@tatw4g`} type='flex' gutter={24}>
                                      <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@4bnd22`}>
                                          <div className="search-title">{labelTime}:</div>
                                      </Col>
                                      <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@0j4cjw`}>
                                           <WeaDateGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaDateGroup@ifivrf`}
                                            isMobx
                                            value={status.value}
                                            datas={status.datas}
                                            onChange={(selectValue, stateDate, endDate)=> this.dateGroupHandle(selectValue, stateDate, endDate)}
                                          />
                                      </Col>
                                    </Row>
                                </Col>
                            </Row>
                        </div>
                    </div>
                     <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@x8bytt`}
                        dragColumns = '0'
                        comsWeaTableStore={tableStoreMyAttent}
                        hasOrder={true}
                        needScroll={true}
                        //isHtml={true}
                        getColumns={c => this.reRenderColumns(c)}
                    />
                </div>
                <div hidden={commentFlag}>
                    <CommentMain ecId={`${this && this.props && this.props.ecId || ''}_CommentMain@rk1qvd`}
                        ref="rightContainer"
                        checkAttention={this.checkAttention}
                        changeGroup={this.changeGroup}
                        topHeight={topHeight}
                        userInfo={userInfo}
                        weiboGroup={weiboGroup}
                        weiboList={weiboList}
                        basicInfo={basicInfo}
                        fanList={fanList}
                        defaultTemplate={defaultTemplate}
                        attentionList={attentionList}
                        systemWorkLog={systemWorkLog}
                        weiboCKEditorConfig={weiboCKEditorConfig}
                        changeReset={this.changeReset}
                        keyWords={keyWords}
                        loading={loading}
                        tab={tab}
                        comment={comment}
                        showSlideEdit={true}
                    />
                </div>
            </div>
        )
    }
    reRenderColumns(columns) {
        columns.forEach(c => {
            c.render = function (text, record) {
                let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
                return <span className='g-table-rerender' dangerouslySetInnerHTML={{ __html: valueSpan }}></span>
            }
        })
        return columns;
    }

    dateGroupHandle(selectValue, stateDate, endDate) {
        if (selectValue[0] === "6") {
            this.props.governReportStore.setItem_three(true)
        } else {
            this.props.governReportStore.setItem_three(false)
        }
        this.props.governReportStore.dateGroupHandle(selectValue);
    }
    browserHandle(ids) {
        this.props.governReportStore.browserHandle(ids);
    }
}
